package com.hui.tally.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hui.tally.R;

public class AddBookDialog extends Dialog implements View.OnClickListener {
    EditText et;
    Button cancelBtn,ensureBtn;
    TextView colortv;
    OnEnsureListener onEnsureListener;
    private RadioGroup radioGroup;
    private RadioButton btn1,btn2,btn3,btn4,btn5,btn6;
    int color;

    // 设定回调接口的方法
    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public AddBookDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_book);//设置对话框显示布局
        et = findViewById(R.id.dialog_add_book_et);
        colortv = findViewById(R.id.select_color);
        cancelBtn = findViewById(R.id.dialog_add_book_btn_cancel);
        ensureBtn = findViewById(R.id.dialog_add_book_ensure);
        radioGroup = findViewById(R.id.dialog_add_book_color_ll);
        btn1 = findViewById(R.id.color_gubook);
        btn2 = findViewById(R.id.color_jiubook);
        btn3 = findViewById(R.id.color_orangebook);
        btn4 = findViewById(R.id.color_yellowbook);
        btn5 = findViewById(R.id.color_dyellowbook);
        btn6 = findViewById(R.id.color_dbluebook);
        radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
        cancelBtn.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
    }
    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            int id = radioGroup.getCheckedRadioButtonId();
            switch (radioGroup.getCheckedRadioButtonId()){
                case R.id.color_gubook:
                    color = R.color.gu;
                    colortv.setText("    " +
                            "    ");
                    colortv.setBackgroundResource(color);
                    break;
                case R.id.color_jiubook:
                    color = R.color.jiu;
                    colortv.setText("    " +
                            "    ");
                    colortv.setBackgroundResource(color);
                    break;
                case R.id.color_orangebook:
                    color = R.color.orange;
                    colortv.setText("    " +
                            "    ");
                    colortv.setBackgroundResource(color);
                    break;
                case R.id.color_yellowbook:
                    color = R.color.yellow;
                    colortv.setText("    " +
                            "    ");
                    colortv.setBackgroundResource(color);
                    break;
                case R.id.color_dyellowbook:
                    color = R.color.dyellow;
                    colortv.setText("    " +
                            "    ");
                    colortv.setBackgroundResource(color);
                    break;
                case R.id.color_dbluebook:
                    color = R.color.dblue;
                    colortv.setText("    " +
                            "    ");
                    colortv.setBackgroundResource(color);
                    break;
            }
        }
    };

    public interface OnEnsureListener{
        public void onEnsure();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_add_book_btn_cancel:
                cancel();
                break;
            case R.id.dialog_add_book_ensure:
                if (onEnsureListener!=null) {
                    onEnsureListener.onEnsure();
                }
                break;
        }
    }

    //    获取输入数据的方法
    public String getEditText(){
        System.out.println(et.getText().toString().trim());
        return et.getText().toString().trim();
    }
    public int getColor(){
        return color;
    }
    /* 设置Dialog的尺寸和屏幕尺寸一致*/
    public void setDialogSize(){
//        获取当前窗口对象
        Window window = getWindow();
//        获取窗口对象的参数
        WindowManager.LayoutParams wlp = window.getAttributes();
//        获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int)(d.getWidth());  //对话框窗口为屏幕窗口
        wlp.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
        handler.sendEmptyMessageDelayed(1,100);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            //自动弹出软键盘的方法
            InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };
}

